﻿using LiveChartsCore;
using LiveChartsCore.Defaults;
using LiveChartsCore.SkiaSharpView;
using LiveChartsCore.SkiaSharpView.Painting;
using MeterReader.PubSubEvents;
using MeterReader.WPF.Services;
using SkiaSharp;
using System.Collections.ObjectModel;

namespace MeterReader.WPF.ViewModels
{
	public class HistoryViewModel : BindableBase
	{
		private readonly IHistory _history;
		private readonly IEventAggregator _eventAggregator;
		private ObservableCollection<DateTimePoint> _dateTimePoints = new();

		public HistoryViewModel(IHistory history, IEventAggregator eventAggregator)
		{
			_history = history;
			_eventAggregator = eventAggregator;
			LiveCharts.Configure(config => config.HasGlobalSKTypeface(SKFontManager.Default.MatchCharacter('汉')));
			Series[0].Values = _dateTimePoints;
			LoadedCommand = new DelegateCommand(Loaded);
			GoBackDataMonitorCommand = new DelegateCommand<string>(GoBackDataMonitor);
			SearchHistoryCommand = new DelegateCommand(SearchHistory, () => EndDateTime > BeginDateTime)
				.ObservesProperty(() => BeginDateTime)
				.ObservesProperty(() => EndDateTime);
		}

		#region properties

		public DelegateCommand LoadedCommand { get; set; }
		public DelegateCommand<string> GoBackDataMonitorCommand { get; set; }
		public DelegateCommand SearchHistoryCommand { get; set; }

		public Axis[] XAxes { get; set; } =
			{
				new DateTimeAxis(TimeSpan.FromHours(1), date => date.ToString("yyyy-MM-dd HH:mm:ss"))
				{
					NameTextSize = 15,
					LabelsRotation = 15,
					NamePaint = new SolidColorPaint(SKColors.WhiteSmoke),
					LabelsPaint = new SolidColorPaint(SKColors.DarkGray),
					TextSize = 15,
					SeparatorsPaint = new SolidColorPaint(SKColors.LightSlateGray) { StrokeThickness = 2 },
				}
			};

		public Axis[] YAxes { get; set; } =
			{
				new Axis()
				{
					Name = "总功率(Kw.h)",
					NameTextSize = 15,
					NamePaint = new SolidColorPaint(SKColors.WhiteSmoke),
					LabelsPaint = new SolidColorPaint(SKColors.DarkGray),
					TextSize = 15,
					MinLimit = 0,
					SeparatorsPaint = new SolidColorPaint(SKColors.LightSlateGray) { StrokeThickness = 2 },
					Labeler = v => $"{v}(Kw.h)"
				}
			};

		public ObservableCollection<ISeries> Series { get; set; } =
			new()
			{
				new LineSeries<DateTimePoint>()
				{
					Fill = null,
					GeometrySize = 0,
					LineSmoothness = 1,
				}
			};

		private DateTime _beginDateTime = DateTime.Now;

		public DateTime BeginDateTime
		{
			get { return _beginDateTime; }
			set { SetProperty(ref _beginDateTime, value); }
		}

		private DateTime _endDateTime = DateTime.Now;

		public DateTime EndDateTime
		{
			get { return _endDateTime; }
			set { SetProperty(ref _endDateTime, value); }
		}

		private List<string> _comBoxSource = new List<string>();

		public List<string> ComBoxSource
		{
			get => _comBoxSource;
			set => SetProperty(ref _comBoxSource, value);
		}

		private string _comBoxSelectedItem;

		public string ComBoxSelectedItem
		{
			get => _comBoxSelectedItem;
			set => SetProperty(ref _comBoxSelectedItem, value);
		}

		#endregion properties

		private async void Loaded()
		{
			ComBoxSource =await _history.GetMetersListAsync("https://localhost:7121/History/GetMeterList");
			ComBoxSelectedItem = ComBoxSource.Any() == true ? _comBoxSource[0] : "falut";
		}

		private void GoBackDataMonitor(string obj) => _eventAggregator.GetEvent<NavigateEvent>().Publish(obj);

		private async void SearchHistory()
		{
			var url = $"https://localhost:7121/History/SearchHistory?name={ComBoxSelectedItem}&start={BeginDateTime}&end={EndDateTime}";
			var list = await _history.SearchHistoryAsync(url);
			if (list.Any())
			{
				var dateTimePoints = list.Select(t => new DateTimePoint(t.Time, t.Value));
				if (dateTimePoints.Any())
				{
					_dateTimePoints!.Clear();
					foreach (var item in dateTimePoints)
					{
						_dateTimePoints!.Add(item);
					}
				}
			}
		}
	}
}